package com.xja.springboot.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
    private static SimpleDateFormat sdf3 = new SimpleDateFormat("yyyyMMdd");
    private static SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy年MM月dd日");
    private static SimpleDateFormat sdf5 = new SimpleDateFormat("yyyy.MM.dd");
    private static SimpleDateFormat[] sdfs = {sdf1,sdf2,sdf3,sdf4,sdf5};
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

    //把字符串转换为日期对象
    public static Date strToDate (String dateStr){
        for (SimpleDateFormat sdf:sdfs){
            try {
                return sdf.parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    //日期转换为字符串
    public static String dateToStr (Date date){
        if (date==null)
            return null;
        return sdf4.format(date);
    }

    public static String newDateToStr(){
        Date date = new Date();
        return sdf3.format(date);
    }

    public static String getNowYyyyMm(){
        Date date = new Date();
        return sdf1.format(date);
    }
}
